Conversation
|
@scaryclam Thank you for your PR! BTW, tests are failed. Could you check? |
| return instances | ||
|
|
||
|
|
||
| def _create_table_object_data(fixture, session): |
There was a problem hiding this comment.
Please add session to docstring param 😉
| create_table(Base) | ||
| fixtures = load_fixture_files( | ||
| self.path, ['accounts.yaml', 'pictures.yaml'], | ||
| self.path, ['accounts.yaml', 'pictures.yaml', 'picture_categories.yaml', 'picture_category_picture.yaml'], |
There was a problem hiding this comment.
Please add new test case, such as test_load_table_fixtures.
I want test_load_fixtures assure not BCBreak 😌
| @@ -0,0 +1,12 @@ | |||
| - table: tests.test_sqlalchey_seed.picture_category_picture | |||
| metadata.reflect(bind=session.get_bind()) | ||
| table = metadata.tables[class_name] | ||
| insert = table.insert() | ||
| session.execute(insert.values(**data['fields'])) |
There was a problem hiding this comment.
I'm not familier with Table so, if I say something wrong with this review, please point out 😉
What happen if insert failed. Does DB rollback?
What about do something like followings?
Is this satisfy your requirements? 😄
I want controll session flush, commit, rollback at same place.
IMO it's readable 😉
def _create_table_object_data(fixture, session):
"""Create a Table object entry.
:param fixture: Fixtures
:param session: Session
"""
table_values = []
for data in fixture:
if 'table' in data:
module_name, class_name = data['table'].rsplit('.', 1)
importlib.import_module(module_name)
metadata = MetaData()
metadata.reflect(bind=session.get_bind())
table = metadata.tables[class_name]
insert = table.insert()
insert.values(**data['fields'])
table_values.append(insert)
return table_values
def load_fixtures(session, fixtures):
"""Load fixture.
:param base: `sqlalchemy.ext.declarative`
:param fixtures: Fixture files
"""
instances = []
tables = []
for fixture in fixtures:
_instances = _create_model_instance(fixture)
for instance in _instances:
instances.append(instance)
_tables = _create_table_object_data(fixture, session)
for table in _tables:
tables.append(table)
try:
for instance in instances:
session.merge(instance)
for table in tables:
session.execute(table)
session.flush()
session.commit()
except Exception:
session.rollback()
raiseThere was a problem hiding this comment.
nn- I think I wrote something strange? 🤔
|
Thank you for PR. Yaml defeinition is LGTM. |
This adds some support for creating sqlalchemy Table object inserts.
This is mainly to aid in the support of M2M relations in projects that use Table rather than Model.
The PR should be considered a WIP for now as I'd like some feedback on whether the approach is acceptable.